<?php 
	namespace Home\Controller;
	use Think\Controller;

	class PublicController extends AuthController
	{
		public function login()
		{
			// if(IS_POST)
	        // {
	            $this->_checkLogin();
	        // }else{
	        	// 登录界面 
	        	// $output['result'] = $this->getState("8");
	        	// // 待返回数据
	        	// $jumpurl = "public/login";
	        	// $output['jumpurl'] = $jumpurl;
	        	// echo json_encode($output);
	        // }
		}
		
		private function _checkLogin(){
			$username = I("username","");
			$password = I("password","",strval);
			$username = trim($username);
			$password = md5(trim($password));

			$user = D("SysUser")->where(array("username"=>$username))->field("id,username,role_id,password,status")->find();
			//dump($user);exit;
			if(!($user && $user['password'] == $password )){
					// $output['result'] = $this->getState("-1");
					echo json_encode($output=0);exit;
				}else{
					if($user['status' != 1 ]){
						// $output['result'] = $this->getState("1");
						echo json_encode($output=0);exit;
					}
					$obj = D("SysUser");
					$data['recent_time'] = time();
					$data['ip'] = $_SERVER["REMOTE_ADDR"];
					$res = $obj->where(array("id"=>$user['id']))->save($data);
					if(!$res){
						// $output['result'] = $this->getState("2");
						echo json_encode($output=0);exit;
					}
					// 当前用户信息存入session  当前用户用户信息
					session("sys_userinfo",serialize($user));
					session("user_id",$user['id']);
					session("role_id",$user['role_id']);
					//dump($_SESSION);exit;
					echo json_encode($output =1);exit;
				}
		}

	    public function logout()
	    {
	        session(null);
	        // $output['result'] = $this->getState("3"); 
	        /*dump(session("user_id"));exit;*/
	       	$output = 1;
			echo json_encode($output);
	    }

	}
 ?>